﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class SelectCollectorDialog : Form
    {
        public SelectCollectorDialog()
        {
            InitializeComponent();
        }
        string lvgPrefix = "Server: ";

        public EventScavengerDAL EventScavengerDAL {get;set;}
        public Collector SelectedCollector { get; set; }

        private void SelectCollectorDialog_Load(object sender, EventArgs e)
        {
            List<Collector> collectors = EventScavengerDAL.GetCollectorList();
            lvwCollectors.Items.Clear();
            foreach (Collector collector in (from Collector col in collectors
                                             orderby col.ReaperName
                                             select col))
            {
                ListViewItem lvi = new ListViewItem(collector.Name);
                lvi.Tag = collector;

                ListViewGroup lvg = (from ListViewGroup g in lvwCollectors.Groups
                                     where g.Header == lvgPrefix + collector.ReaperName
                                     select g).FirstOrDefault();
                if (lvg == null)
                {
                    lvg = new ListViewGroup(lvgPrefix + collector.ReaperName);
                    lvg.Tag = collector.ReaperName;
                    lvwCollectors.Groups.Add(lvg);
                }
                lvi.Group = lvg;
                lvwCollectors.Items.Add(lvi);
            }
        }

        private void lvwCollectors_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmdOK.Enabled = lvwCollectors.SelectedItems.Count > 0;
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (lvwCollectors.SelectedItems.Count > 0)
            {
                SelectedCollector = (Collector)lvwCollectors.SelectedItems[0].Tag;
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }

    }
}
